//
//  HX_CustomTableCell.m
//  UI10-CustomTableViewCell
//
//  Created by 韩旭 on 16/7/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_CustomTableCell.h"
//使用@class声明的类, 需要在.m中去import
#import "NewsObject.h"

@interface HX_CustomTableCell ()
@property (nonatomic, strong) UIView *view;
@end
@implementation HX_CustomTableCell

// 自定义TableViewcell的初始化方法
// initWithStyle：reuseIdentifier
// 注意：不要使用initWithFrame


- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _imageTitle.image = [UIImage imageNamed:newsObject.newsImage];
    _lable.text = newsObject.titleName;
    _lableDetail.text = newsObject.Description;
    _lableSmall.text = newsObject.newsNumber;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        TableViewCell 默认的大小宽320 高44
//        NSLog(@"初始化时, cell的frame是 %@", NSStringFromCGRect(self.frame));
//        在初始化时,cell 的frame是初始值. 不能为自定义视图布局
//        添加子视图View
       _view = [[UIView alloc] initWithFrame:CGRectZero];
//        初始化方法中，只负责创建
//        3. 创建的子视图应该添加在哪个父视图
       _view.backgroundColor = [UIColor orangeColor];
//        contentView显示内容
        [self.contentView addSubview:_view];
        _imageTitle = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_imageTitle];
        _lable = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_lable];
        
        _lableDetail = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_lableDetail];
        
        _lableSmall = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.lableDetail addSubview:_lableSmall];
    }
    return self;
}
// 布局子视图及它本身
- (void)layoutSubviews {
//    千万不要忘记 super layoutSubviews
    [super layoutSubviews];
//    NSLog(@"%s初始化时, cell的frame是%@", __FUNCTION__,NSStringFromCGRect(self.frame));
//    不要使用cell的frame去进行视图的布局
//    使用contentView的frame进行布局
    _view.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 100);
//    layousubview中只，负责给视图布局
//    练习
    _imageTitle.frame = CGRectMake(10, 10, self.contentView.frame.size.width * 0.2, self.contentView.frame.size.height - 20);
    _lable.frame = CGRectMake(_imageTitle.frame.size.width + _imageTitle.frame.origin.x + 10, _imageTitle.frame.origin.y, self.contentView.frame.size.width - (_imageTitle.frame.size.width + _imageTitle.frame.origin.x + 20), self.contentView.frame.size.height - 120);
    _lableDetail.frame = CGRectMake(_lable.frame.origin.x, _lable.frame.size.height + _lable.frame.origin.y + 10, _lable.frame.size.width, _lable.frame.size.height );
//    lable上不要添加视图
//    视图控制器转化
//    1. 判断父视图是不是_lableDetail
//    if (_lableSmall.superview == _lableDetail) {
//    2. 改numberContsLabel的父视图
    [_lableDetail addSubview:_lableSmall];
        _lableSmall.frame = CGRectMake(100, 20, 100, 20);
    CGRect newRect = [_lableSmall.superview convertRect:_lableSmall.frame toView:self.contentView];
    _lableSmall.frame = newRect;
    [self.contentView addSubview:_lableSmall];
//    }
}

@end
